using UnityEngine;
using System.Collections.Generic;

public class ElevatorScript : MonoBehaviour {

    private float touchShift;
    private Touch touch;
    
	private Vector3 leftPosition;
	private Vector3 centerPosition;
	private Vector3 rightPosition;
	private Vector3 displacementPosition;
	private float angle;
	private bool bPositionCanChange;
	public int actualPosition;
	
	
	public float velocity;
	
	public enum State{left, center, right, idle};
    public State state;
    
	public float percentageXofScreen;
	public float tracksSpacingPercentage;
	public float topPositionPercentage;
	
    public void Start(){
		GameScript.loadGameVariables();
		//how much you need to drag your finger to be a valid movement
        touchShift = 10.5f;
		//just falling down for default. You begin stopped
        state = State.idle;
		bPositionCanChange = true;
		//POSITIONING on the top
        transform.position += Vector3.up * (MainScript.scaleFromScreen(topPositionPercentage) -
			((collider.bounds.size.y / 4.0f)*((float)Screen.width / (float)Screen.height)));
		//SCALING related to public variables setted on the engine
		transform.localScale = MainScript.scaleFromScreen(gameObject, percentageXofScreen);//1.5f proportion from X to Y
		//Angle to extract sin function to elevator idle movement
		angle = 0;
		//Wich column you are
		//left = 0; center = 1; right = 2;
		actualPosition = 1;
		//load tracks positions (left center and right)
		displacementPosition = centerPosition+((Vector3.right * (collider.bounds.size.x)) +
			(Vector3.right * MainScript.scaleFromScreen(tracksSpacingPercentage)));
		centerPosition = transform.position;
		leftPosition = centerPosition+((Vector3.left * (collider.bounds.size.x)) +
			(Vector3.left * MainScript.scaleFromScreen(tracksSpacingPercentage)));
		rightPosition = centerPosition+((Vector3.right * (collider.bounds.size.x)) +
			(Vector3.right * MainScript.scaleFromScreen(tracksSpacingPercentage)));
    }

	void move ()
	{
		if (state == State.right) {
			if (bPositionCanChange) {
				actualPosition = 2;
				bPositionCanChange = false;
			}
			Vector3 direction = rightPosition - transform.position;
			float distance = direction.magnitude;
			
			transform.Translate(direction*(Time.deltaTime*velocity),Space.World);
			
			if (distance < 0.1f) {
				state = State.idle;
				actualPosition=2;
			}
			else if (distance < 1.0f) {
				bPositionCanChange = true;
			}
		}
		if(state == State.center) {
			if (bPositionCanChange) {
				actualPosition = 1;
				bPositionCanChange = false;
			}
			Vector3 direction = centerPosition - transform.position;
			float distance = direction.magnitude;
			
			transform.Translate(direction*(Time.deltaTime*velocity),Space.World);
			if (distance < 0.1f) {
				state = State.idle;
				actualPosition=1;
			}
			else if (distance < 1.0f) {
				bPositionCanChange = true;
			}
		}
		if(state == State.left) {
			if (bPositionCanChange) {
				actualPosition = 0;
				bPositionCanChange = false;
			}
			Vector3 direction = leftPosition - transform.position;
			float distance = direction.magnitude;
			
			transform.Translate(direction*(Time.deltaTime*velocity),Space.World);
			if (distance < 0.1f) {
				state = State.idle;
				actualPosition=0;
			}
			else if (distance < 1.0f) {
				bPositionCanChange = true;
			}
		}
	}
	
    void Update()
    {
        captureMovement();
        captureMovementPC();
		move();
		
    }

    public void captureMovement(){//ON ANDROID: Touch & drag (left or right) to move the elevator
        if (Input.touchCount > 0)
        {
            touch = Input.GetTouch(0);
            //move left
            if (touch.deltaPosition.x < 0 - touchShift)
            {
				if (actualPosition==2) {
					state = State.center;
				}
				else if (actualPosition==1) {
					state = State.left;
				}
            }
            //move right
            else if (touch.deltaPosition.x > 0 + touchShift)
            {
                if (actualPosition==0) {
					state = State.center;
				}
				else if (actualPosition==1) {
					state = State.right;
				}
            }
        }
    }
	
	//LEFT ARROW and RIGHT ARROW for left and right to move the elevator
    public void captureMovementPC()
    {
        if (SystemInfo.deviceType == DeviceType.Console || SystemInfo.deviceType == DeviceType.Desktop) {
			//move left PC
            if(Input.GetKeyDown(KeyCode.LeftArrow)) {
				if (actualPosition==2) {
					state = State.center;
				}
				else if (actualPosition==1) {
					state = State.left;
				}
			}
			//move right PC
			else if(Input.GetKeyDown(KeyCode.RightArrow)) {
				if (actualPosition==0) {
					state = State.center;
				}
				else if (actualPosition==1) {
					state = State.right;
				}
			}
		}
    }

    void OnTriggerEnter(Collider c)
    {//only coins are trigger
        if (!c.isTrigger)
        {
            StopAllCoroutines();
            GameScript.gameOver();
        }
    }
}